package com.j2205.hongzhiyuan.service.impl;

import com.j2205.hongzhiyuan.mapper.RoleMapper;
import com.j2205.hongzhiyuan.mapper.UserMapper;
import com.j2205.hongzhiyuan.pojo.Role;
import com.j2205.hongzhiyuan.pojo.UserInfo;
import com.j2205.hongzhiyuan.service.UserService;
import com.j2205.hongzhiyuan.util.MybatisUtil;
import com.j2205.hongzhiyuan.vo.UserInfoVo;

import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public boolean login(String username, String password) {
        UserMapper userMapper= MybatisUtil.getSession().getMapper(UserMapper.class);
        UserInfo userInfo=userMapper.findUserByName(username);
        MybatisUtil.clseAll();
        if(userInfo != null){
            if(userInfo.getPassword().equals(password)){

                return true;
            }else{
                return false;
            }

        }else{
            return false;
        }



    }

    @Override
    public UserInfoVo findUserInfoVoByName(String username) {
        UserInfoVo infoVo = new UserInfoVo();
        UserMapper userInfoMapper = MybatisUtil.getSession().getMapper(UserMapper.class);
        UserInfo userInfo = userInfoMapper.findUserByName(username);
        infoVo.setInfo(userInfo);
        RoleMapper roleMapper = MybatisUtil.getSession().getMapper(RoleMapper.class);
        List<Role> allRoles = roleMapper.findAllRoles();
        infoVo.setRoles(allRoles);
        MybatisUtil.clseAll();
        return infoVo;
    }

    @Override
    public boolean update(UserInfo userInfo) {
        UserMapper mapper = MybatisUtil.getSession().getMapper(UserMapper.class);
        int i = mapper.updateUser(userInfo);
        if (i>-1){
            MybatisUtil.commit();
            MybatisUtil.clseAll();
            return true;
        }
        return false;
    }

    @Override
    public UserInfo findUserById(int usID) {
        UserMapper mapper = MybatisUtil.getSession().getMapper(UserMapper.class);
        UserInfo userInfo = mapper.findUserById(usID);
        MybatisUtil.clseAll();
        return userInfo;
    }
}
